public class scenario1_java_string
{
	public static void main(String[] args) {
		
		System.out.println("\nResults for intern() method test: ");
		testIntern();

		/* Test the time "+ operator" used
		 */

		long startTime = System.currentTimeMillis();
		String result = null;
		for (int i = 0; i < 5000; i++) {
			result = result + "hello, " + "world!\n";
		}
		long endTime = System.currentTimeMillis();
		System.out.println("Time used: " + (endTime-startTime) + " milli seconds");
				
		/* Test the time "StringBuffer" used
		 */
		startTime = System.currentTimeMillis();
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < 5000; i++) {
			buf.append("hello, ");
			buf.append("world!\n");
		}
		endTime = System.currentTimeMillis();
		System.out.println("Time used: " + (endTime - startTime) + " milli seconds");

		/*		
		System.out.println("\nResults in compare ver 2: ");
		compare_ver2();
		*/

	}
	
	/* Test method differs from that in main()
	 */
	public static void compare_ver2() {
		/* Test the time "+ operator" used
		 */
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < 5000; i++) {
			String result = "hello, " + "world!\n";
		}
		long endTime = System.currentTimeMillis();
		System.out.println("Time used: " + (endTime-startTime) + " milli seconds");
				
		/* Test the time "StringBuffer" used
		 */
		startTime = System.currentTimeMillis();
		for (int i = 0; i < 5000; i++) {
			StringBuffer buf = new StringBuffer();
			buf.append("hello, ");
			buf.append("world!\n");
		}
		endTime = System.currentTimeMillis();
		System.out.println("Time used: " + (endTime - startTime) + " milli seconds");
	}

	/* demostrate the importance of String.intern() method
	 */
	public static void testIntern() {
		String vars[] = new String[5000];
		
		for (int i = 0; i < vars.length; i++) {
			vars[i] = "s" + i;
		}
		
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < vars.length; i++) {
			vars[i] = "hello";
		}
		long endTime = System.currentTimeMillis();
		System.out.println("Time taken: " + (endTime-startTime) + " milli seconds");
		
		startTime = System.currentTimeMillis();
		for (int i = 0; i < vars.length; i++) {
			vars[i] = new String("hello");
		}
		endTime = System.currentTimeMillis();
		System.out.println("Time taken: " + (endTime-startTime) + " milli seconds");

		/* String.intern(): string pool is hold by String class, 
		 *                  so there only exist one pool in the memory,
		 *                  no matter how many strings there are.
		 *        every string has an index in the pool
		 */
		startTime = System.currentTimeMillis();
		for (int i = 0; i < vars.length; i++) {
			vars[i] = new String("hello");
			vars[i] = vars[i].intern();
		}
		if (vars[0] == vars[250])
			System.out.println("equal");
		endTime = System.currentTimeMillis();
		System.out.println("Time taken: " + (endTime-startTime) + " milli seconds");
	}
}
